package com.kujie.access.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kujie.access.domain.query.PayManageQuery;
import com.kujie.access.domain.vo.PayManageVo;
import com.kujie.access.service.IPayService;
import com.kujie.common.core.domain.R;
import com.kujie.common.core.utils.StringUtils;
import com.kujie.common.core.validate.AddGroup;
import com.kujie.common.core.validate.EditGroup;
import com.kujie.common.encrypt.annotation.ApiEncrypt;
import com.kujie.common.excel.utils.ExcelUtil;
import com.kujie.common.mybatis.core.page.PageQuery;
import com.kujie.common.web.core.BaseController;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 缴费管理 api
 *
 * @author jyy
 * @date 2025-05-26
 */
@RestController
@RequestMapping("/pay")
@RequiredArgsConstructor
@ApiEncrypt(response = true)
public class PayController extends BaseController {

    private final IPayService service;

    /**
     * 获取缴费管理分页
     *
     * @param query     入参
     * @param pageQuery 分页入参
     * @return R<?>
     */
    @GetMapping("/getPayManagePage")
    public R<Page<PayManageVo>> getPayManagePage(PayManageQuery query, PageQuery pageQuery) {
        Page<PayManageVo> result = service.getPayManagePage(query, pageQuery);
        return R.ok(result);
    }

    /**
     * 获取缴费管理详情
     *
     * @param id 主键id
     * @return 缴费详情
     */
    @GetMapping("/detail")
    public R<PayManageVo> detail(Long id) {
        return R.ok(service.detail(id));
    }

    /**
     * 新增缴费管理
     *
     * @param query 入参
     * @return R<?>
     */
    @PostMapping("/addPayManage")
    public R<?> addPayManage(@Validated(AddGroup.class)
                             @RequestBody PayManageQuery query) {
        return toAjax(service.addPayManage(query, true));
    }

    /**
     * 修改缴费管理
     *
     * @param query 入参
     * @return R<?>
     */
    @PutMapping("/updatePayManage")
    public R<?> updatePayManage(@Validated(EditGroup.class)
                                @RequestBody PayManageQuery query) {
        return toAjax(service.updatePayManage(query, true));
    }

    /**
     * 删除缴费管理
     *
     * @param ids 入参
     * @return R<?>
     */
    @DeleteMapping("/deletePayManage/{ids}")
    public R<?> deletePayManage(@NotEmpty(message = "主键不能为空")
                                @PathVariable List<Long> ids) {
        return toAjax(service.deletePayManage(ids, true));
    }

    /**
     * 导出缴费管理
     *
     * @param query    入参
     * @param response 响应
     */
    @PostMapping("/exportPayManage")
    public void exportPayManage(@RequestBody PayManageQuery query, HttpServletResponse response) {
        List<PayManageVo> result = service.getPayManageList(query);
        ExcelUtil.exportExcel(result, "缴费管理", PayManageVo.class, response);
    }

    /**
     * 导出缴费管理模板
     *
     * @param response 响应
     */
    @PostMapping("/exportPayManageTemplate")
    public void exportPayManageTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(CollectionUtil.newArrayList(), "缴费管理导入模板", PayManageVo.class, response);
    }

    /**
     * 导入缴费管理模板
     *
     * @param file 文件流
     * @return R<?>
     */
    @PostMapping(value = "/importPayManage", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<?> importPayManage(@RequestPart("file") MultipartFile file) {
        String result = service.importPayManage(file);
        return StringUtils.isEmpty(result) ? R.fail() : R.ok(result);
    }
}
